//
//  ForgetPassWordSecondStepTableViewController.swift
//  ZNHXManagement
//
//  Created by 四川中农 on 2018/9/28.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class ForgetPassWordSecondStepTableViewController: BaseTableViewController {
    
    @IBOutlet weak var verCodeField: UITextField!
    @IBOutlet weak var passwordField: UITextField!
    var phone = ""
    
    lazy var alertView : AlertContentView = {
        let view = AlertContentView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        
        view.leftClosure = { [weak self] in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        
        UIApplication.shared.keyWindow?.addSubview(view)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        self.title = "忘记密码"
        self.passwordField.isSecureTextEntry = true
        self.verCodeField.addTarget(self, action: #selector(valueChange(textField:)), for: .editingChanged)
        self.passwordField.addTarget(self, action: #selector(valueChange(textField:)), for: .editingChanged)
        
        self.passwordField.setValue(UIColor.init(hexColor: "#cccccc"), forKeyPath: "_placeholderLabel.textColor")
        self.verCodeField.setValue(UIColor.init(hexColor: "#cccccc"), forKeyPath: "_placeholderLabel.textColor")
        self.passwordField.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    @objc func valueChange(textField:UITextField){
        var maxLength = 0
        if textField == self.verCodeField {
            maxLength = 4
        }else{
            maxLength = 12
        }
        let toBeString = textField.text!
        let lang = textField.textInputMode?.primaryLanguage
        if lang == "zh-Hans" {
            let selectedRange = textField.markedTextRange
            if selectedRange == nil && toBeString.count > maxLength{
                let endIndex = toBeString.index(toBeString.startIndex, offsetBy: maxLength)
                textField.text = String(toBeString[..<endIndex])
            }
        }else if toBeString.count > maxLength {
            let endIndex = toBeString.index(toBeString.startIndex, offsetBy: maxLength)
            textField.text = String(toBeString[..<endIndex])
        }
    }

    // MARK: 确定
    @IBAction func confirmButtonClick(_ sender: UIButton) {
        self.view.endEditing(true)
        PKhud.show()
        HttpRequest.requestData(.post, URLString: Api.reset_password.rawValue, parameters: ["username":self.phone,"password":self.passwordField.text!,"code":self.verCodeField.text!]) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                self.alertView.showSuccessAlert()
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
    
    // MARK:是否明文显示密码
    @IBAction func isShowPassWord(_ sender: UIButton) {
        if !sender.isSelected {
            self.passwordField.isSecureTextEntry = false
        }else{
            self.passwordField.isSecureTextEntry = true
        }
        sender.isSelected = !sender.isSelected
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }

}

extension ForgetPassWordSecondStepTableViewController:UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // 只能输入数字跟字母
        if string == "" {
            return true
        }
        let char = (string as NSString).character(at: 0)
        if char < 48 {
            return false
        }
        if char > 57 && char < 65 {
            return false
        }
        if char > 90 && char < 97 {
            return false
        }
        if char > 122 {
            return false
        }
        return true
    }
    
}


















